package com.hitqz.robot.daemon.quartz.schedule;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.entity.GoodsBuffersEntity;
import com.hitqz.robot.api.business.entity.JobCachesEntity;
import com.hitqz.robot.biz.service.GoodsBuffersService;
import com.hitqz.robot.biz.service.JobCachesService;
import com.hitqz.robot.daemon.quartz.constants.RobotQuartzEnum;
import com.hitqz.robot.daemon.quartz.util.MixPosTaskUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * @author xupkun
 * @date 2025/4/11
 */
@Component("mixPosTaskSchedule")
@RequiredArgsConstructor
@Slf4j
public class MixPosTaskSchedule {

    private final RestTemplate restTemplate;

    private final GoodsBuffersService goodsBuffersService;

    private final JobCachesService jobCachesService;

    /**
     * 京天威 定时任务请求接口 交接位置的信息
     * @return
     */
    public String execute() {
        for (String src : MixPosTaskUtil.requestUri.keySet()) {
            synchronized (src.intern()) {
                String result = "";
                try {
                    GoodsBuffersEntity goodsBuffersEntity = goodsBuffersService.getOne(Wrappers.<GoodsBuffersEntity>lambdaUpdate()
                            .eq(GoodsBuffersEntity::getStoreName, src).last("limit 1"));
                    if (goodsBuffersEntity != null) {
                        if (goodsBuffersEntity.getStoreStatus() == 0){
                            result = restTemplate.getForObject(MixPosTaskUtil.requestUri.get(src), String.class);
                            JSONObject jsonObject = JSONObject.parseObject(result);
                            if (jsonObject.containsKey("data")) {
                                JSONObject data = jsonObject.getJSONObject("data");
                                if (data.containsKey("pjid")) {
                                    String pjid = data.getString("pjid");
                                    String lastPjId = MixPosTaskUtil.taskPjId.get(src);
                                    if (StrUtil.isEmpty(lastPjId) || !pjid.trim().equals(lastPjId.trim())) {
                                        String des = data.getString("des");
                                        String taskUri = "http://192.10.11.153:9999/admin/km/agv/call?src="
                                                + src + "&des=" + des + "&pjid=" + pjid + "&pjid1=" + pjid;
                                        String taskName = createTaskName(src, des, true);
                                        JobCachesEntity jobCachesEntity = jobCachesService.getOne(Wrappers.<JobCachesEntity>lambdaQuery()
                                                .eq(JobCachesEntity::getTaskName, taskName)
                                                .last("limit 1")
                                        );
                                        if (jobCachesEntity == null) {
                                            goodsBuffersEntity.setStoreStatus(2);
                                            goodsBuffersService.updateById(goodsBuffersEntity);
                                            MixPosTaskUtil.taskPjId.put(src, pjid);
                                            String taskResult = restTemplate.postForObject(taskUri, "", String.class);
                                            log.info("交接位置任务请求地址: {}--{}- pjid: {}- lastpjid: {}", taskUri, taskResult, pjid, lastPjId);
                                        }
                                    }
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    log.info("{} 请求交接位置失败结果: {} 失败:{}",src,  result, e.getMessage());
                }
            }
            }

        return RobotQuartzEnum.JOB_LOG_STATUS_SUCCESS.getType();
    }


    public String createTaskName(String src, String des, boolean returnTaskName) {
        String taskName = "";

        String uri = "http://ip:9999/admin/km/agv/call/dispatch?src=" + src + "&des=" + des + "&groupId=executeGroupId";
        if (src.equals("01N01") && des.equals("01N03")) {
            // 双轮对
            taskName = "轮轴分离-轮轴收入";
            uri = uri.replace("ip", "localhost").replace("executeGroupId", "11");
        }else if (src.equals("45NX") && des.equals("")) {
            // 双轮对
            taskName = "轮轴支出-临修";
            uri = uri.replace("ip", "192.10.11.152").replace("executeGroupId", "6");
        }  else if (src.equals("45NX") && des.equals("45N01")) {
            // 双轮对
            taskName = "轮轴支出-60T";
            uri = uri.replace("ip", "192.10.11.152").replace("executeGroupId", "6");
        } else if (src.equals("45NX") && des.equals("45N02")) {
            // 双轮对
            taskName = "轮轴支出-70T";
            uri = uri.replace("ip", "192.10.11.152").replace("executeGroupId", "6");
        } else if (src.equals("20S04") && des.equals("20S05")) {
            // 返厂AGV
            taskName =  "22S前返厂暂停位置";
            uri = uri.replace("ip", "192.10.11.153").replace("executeGroupId", "14");
        } else if (src.equals("22S05") && des.equals("22S06")) {
            // 单轮对
            taskName = "人工复探(南)-压装";
            uri = uri.replace("ip", "192.10.11.153").replace("executeGroupId", "17");
        } else if (src.equals("22S05") && des.equals("22N06")) {
            // 单轮对
            taskName = "人工复探(南)-镟修";
            uri = uri.replace("ip", "192.10.11.153").replace("executeGroupId", "17");
        }  else if (src.equals("22S05") && (des.equals("566"))) {
            // 单轮对
            taskName = "人工复探(南)-缓存架";
            uri = uri.replace("ip", "192.10.11.153").replace("executeGroupId", "17");
        } else if (src.equals("22N05") && des.equals("566")) {
            // 单轮对
            taskName = "人工复探(北)-缓存架";
            uri = uri.replace("ip", "192.10.11.153").replace("executeGroupId", "6");
        } else if (src.equals("566") && des.equals("40N01")) {
            // 单轮对
            taskName = "缓存架-关盖(北)";
            uri = uri.replace("ip", "192.10.11.153").replace("executeGroupId", "10");
        } else if (src.equals("566") && des.equals("40S01")) {
            // 单轮对
            taskName = "缓存架-关盖(南)";
            uri = uri.replace("ip", "192.10.11.153").replace("executeGroupId", "10");
        }  else if (src.equals("22N05") && des.equals("22N06")) {
            // 单轮对
            taskName = "人工复探(北)-镟修";
            uri = uri.replace("ip", "192.10.11.153").replace("executeGroupId", "6");
        } else if (src.equals("22N05") && des.equals("22S06")) {
            // 单轮对
            taskName = "人工复探(北)-压装";
            uri = uri.replace("ip", "192.10.11.153").replace("executeGroupId", "6");
        } else if (src.equals("22N08") && des.equals("40S01")) {
            taskName =  "镟修-关盖(南)";
            uri = uri.replace("ip", "192.10.11.153").replace("executeGroupId", "10");
        } else if (src.equals("22N08") && des.equals("40N01")) {
            taskName =  "镟修-关盖(北)";
            uri = uri.replace("ip", "192.10.11.153").replace("executeGroupId", "10");
        } else if (src.equals("34S02") && des.equals("40S01")) {
            taskName =  "压装(南)-关盖(南)";
            uri = uri.replace("ip", "192.10.11.153").replace("executeGroupId", "16");
        } else if (src.equals("34N04") && des.equals("40N01")) {
            taskName =  "压装(北)-关盖(北)";
            uri = uri.replace("ip", "192.10.11.153").replace("executeGroupId", "16");
        } else if (src.equals("34S02") && des.equals("40N01")) {
            taskName =  "压装(南)-关盖(北)";
            uri = uri.replace("ip", "192.10.11.153").replace("executeGroupId", "16");
        } else if (src.equals("34N04") && des.equals("40S01")) {
            taskName =  "压装(北)-关盖(南)";
            uri = uri.replace("ip", "192.10.11.153").replace("executeGroupId", "16");
        }else if (src.equals("22N05") && des.equals("40S01")) {
            taskName =  "人工复探(北)-关盖(南)";
        } else if (src.equals("22N05") && des.equals("40N01")) {
            taskName =  "人工复探(北)-关盖(北)";
        } else if (src.equals("22S05") && des.equals("40S01")) {
            taskName =  "人工复探(南)-关盖(南)";
        } else if (src.equals("22S05") && des.equals("40N01")) {
            taskName =  "人工复探(南)-关盖(北)";
        } else if (src.equals("22N07") && des.equals("30S01")) {
            taskName =  "镟修-压装-70T";
            uri = uri.replace("ip", "192.10.11.154").replace("executeGroupId", "6");
        } else if (src.equals("22N07") && des.equals("30N01")) {
            taskName =  "镟修-压装-60T";
            uri = uri.replace("ip", "192.10.11.154").replace("executeGroupId", "6");
            //} else if (src.equals("") && des.equals("")) {
            //    taskName = "关盖换料(60T)";
            //    uri = uri.replace("ip", "192.10.11.153").replace("executeGroupId", "20");
            //} else if (src.equals("") && des.equals("")) {
            //    taskName = "关盖换料(70T)";
            //    uri = uri.replace("ip", "192.10.11.153").replace("executeGroupId", "20");
        } else if (src.equals("") &&
                (des.equals("5232") || des.equals("5233") || des.equals("5234") ) ) {
            // 压装间 轴承替换
            taskName = "压装间托盘替换";
            uri = uri.replace("ip", "192.10.11.154").replace("executeGroupId", "6");
        } else if ((src.equals("5235") || src.equals("5236"))
                && (des.equals("5232") || des.equals("5233") || des.equals("5234"))) {
            taskName = "压装间托盘替换(分解)";
            uri = uri.replace("ip", "192.10.11.154").replace("executeGroupId", "6");
        } else if (src.equals("") &&  (des.equals("5211") || des.equals("5210") || des.equals("5209")
                || des.equals("5208") || des.equals("5207") || des.equals("5206") || des.equals("5205") || des.equals("5204")
                || des.equals("5203"))) {
            taskName = "轴承推卸智能替换";
            uri = uri.replace("ip", "192.10.11.155").replace("executeGroupId", "6");
        } else if (src.equals("") &&  ( des.equals("5202") || des.equals("5201"))) {
            taskName = "后挡料箱推卸替换";
            uri = uri.replace("ip", "192.10.11.155").replace("executeGroupId", "6");
        }
        return returnTaskName ? taskName : uri;
    }



}
